package com.xingfudazi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingfudazi.acl.cache.CacheClient;
import com.xingfudazi.app.domain.AppMessage;
import com.xingfudazi.app.domain.AppMoment;
import com.xingfudazi.app.domain.AppNotice;
import com.xingfudazi.app.mapper.AppNoticeMapper;
import com.xingfudazi.app.service.IAppNoticeService;
import com.xingfudazi.common.enums.DBDataStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 通知Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppNoticeServiceImpl implements IAppNoticeService {
    @Autowired
    private AppNoticeMapper appNoticeMapper;

    @Autowired
    private CacheClient cacheClient;

    private final static String KEY = "APP_NOTICE_KEY:";

    /**
     * 查询通知
     *
     * @param id 通知主键
     * @return 通知
     */
    @Override
    public AppNotice selectAppNoticeById(Long id) {
        return appNoticeMapper.selectById(id);
    }

    /**
     * 查询通知列表
     *
     * @param appNotice 通知
     * @return 通知
     */
    @Override
    public List<AppNotice> selectAppNoticeList(AppNotice appNotice) {
        LambdaQueryWrapper<AppNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppNotice::getCreateTime);
        return appNoticeMapper.selectList(wrapper);
    }

    /**
     * 新增通知
     *
     * @param appNotice 通知
     * @return 结果
     */
    @Override
    public int insertAppNotice(AppNotice appNotice) {
        int insert = appNoticeMapper.insert(appNotice);
        // 存储30s
        cacheClient.setCacheObject(KEY + appNotice.getId(), appNotice, 60, TimeUnit.SECONDS);
        return insert;
    }

    /**
     * 修改通知
     *
     * @param appNotice 通知
     * @return 结果
     */
    @Override
    public int updateAppNotice(AppNotice appNotice) {
        return appNoticeMapper.updateById(appNotice);
    }

    /**
     * 删除通知信息
     *
     * @param id 通知主键
     * @return 结果
     */
    @Override
    public int deleteAppNoticeById(Long id) {
        AppNotice appNotice = new AppNotice();
        appNotice.setDelFlag(DBDataStatus.DELETED.getCode());
        appNotice.setId(id);
        return appNoticeMapper.updateById(appNotice);
    }

    @Override
    public List<AppNotice> polling() {
        Set<String> keys = cacheClient.keys(KEY+"*");
        if (keys.size() > 10) {
            keys = truncateSet(keys, 10);
        }

        return keys.stream().map(key -> cacheClient.<AppNotice>getCacheObject(key)).collect(Collectors.toList());
    }

    public Set<String> truncateSet(Set<String> set, int maxLength) {
        return set.stream()
                .map(element -> element.length() <= maxLength ? element : element.substring(0, maxLength))
                .collect(Collectors.toSet());
    }
}
